// packageA/pages/coupon/index.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        currentTab: 0,
        searchValue: '',
        couponList: [],
        filteredList: [],
        couponTypes: [
          { label: '全部', value: 'all' },
          { label: '满减券', value: 'full_reduce' },
          { label: '直减券', value: 'direct_reduce' },
          { label: '折扣券', value: 'discount' },
          { label: '已过期', value: 'expired' }
        ],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.loadCouponList();
    },
    onPullDownRefresh() {
        this.loadCouponList();
        wx.stopPullDownRefresh();
      },
    
      onReachBottom() {
        this.loadMoreData();
      },
    
      // 加载优惠券列表
      loadCouponList() {
        // 模拟数据
        const mockData = [
          {
            id: 1,
            name: '新用户专享券',
            type: 'direct_reduce',
            typeText: '直减券',
            reduceAmount: 10,
            status: 'active',
            startTime: '2025-11-01',
            endTime: '2025-11-30'
          },
          {
            id: 2,
            name: '满100减20',
            type: 'full_reduce',
            typeText: '满减券',
            fullAmount: 100,
            reduceAmount: 20,
            status: 'active',
            startTime: '2025-11-01',
            endTime: '2025-11-30'
          },
          {
            id: 3,
            name: '秋季折扣券',
            type: 'discount',
            typeText: '折扣券',
            discount: 8.5,
            status: 'expired',
            startTime: '2025-10-01',
            endTime: '2025-10-31'
          }
        ];
    
        this.setData({
          couponList: mockData,
          filteredList: mockData
        });
      },
    
      // 切换分类标签
      switchTab(e) {
        const tab = parseInt(e.currentTarget.dataset.tab);
        this.setData({ currentTab: tab });
        this.filterCouponList();
      },
    
      // 搜索输入
      onSearchInput(e) {
        this.setData({ searchValue: e.detail.value });
        this.filterCouponList();
      },
    
      // 筛选优惠券列表
      filterCouponList() {
        const { couponList, currentTab, searchValue } = this.data;
        
        let filtered = couponList.filter(item => {
          // 搜索筛选
          if (searchValue && !item.name.includes(searchValue)) {
            return false;
          }
          
          // 分类筛选
          switch (currentTab) {
            case 1: return item.type === 'full_reduce';
            case 2: return item.type === 'direct_reduce';
            case 3: return item.type === 'discount';
            case 4: return item.status === 'expired';
            default: return true;
          }
        });
    
        this.setData({ filteredList: filtered });
      },
    
      // 编辑优惠券
      onEdit(e) {
        const id = e.currentTarget.dataset.id;
        wx.navigateTo({
          url: `/packageA/pages/create_coupon/index?id=${id}`
        });
      },
    
      // 删除优惠券
      onDelete(e) {
        const id = e.currentTarget.dataset.id;
        
        wx.showModal({
          title: '确认删除',
          content: '确定要删除这个优惠券吗？',
          success: (res) => {
            if (res.confirm) {
              this.deleteCoupon(id);
            }
          }
        });
      },
    
      // 删除优惠券逻辑
      deleteCoupon(id) {
        const newList = this.data.couponList.filter(item => item.id !== id);
        this.setData({ 
          couponList: newList,
          filteredList: newList
        });
        
        wx.showToast({
          title: '删除成功',
          icon: 'success'
        });
      },
    
      // 跳转到创建页面
      goToCreate() {
        wx.navigateTo({
          url: '/packageA/pages/create_coupon/index'
        });
      },
    
      // 加载更多数据
      loadMoreData() {
        // 模拟加载更多数据
        wx.showLoading({ title: '加载中...' });
        
        setTimeout(() => {
          const newCoupons = [
            {
              id: Date.now(),
              name: '新增优惠券',
              type: 'direct_reduce',
              typeText: '直减券',
              reduceAmount: 5,
              status: 'active',
              startTime: '2025-11-01',
              endTime: '2025-11-30'
            }
          ];
          
          this.setData({
            couponList: [...this.data.couponList, ...newCoupons],
            filteredList: [...this.data.filteredList, ...newCoupons]
          });
          
          wx.hideLoading();
        }, 1000);
      },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    // onPullDownRefresh() {

    // },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})